{% extends "repo/repo.html" %}

{% block subcontainer %}
<div class="bubble inner">
<style></style>

<div id="id_merge_process" class="hide colspan">
  <h2 class="heading">
  &gt; 将仓库分支
{{pullRequest.source_repo.username}}/{{pullRequest.source_repo.name}}:{{pullRequest.source_refname}}
  合并到
{{pullRequest.desc_repo.username}}/{{pullRequest.desc_repo.name}}:{{pullRequest.desc_refname}} 
  </h2>
  <div class="span10">
    <pre id="id_merge_output" class="shell"></pre>
  </div>
  <div class="span10">
    <a id="id_merge_after" class="hide" href="/{{user_name}}/{{repo_name}}/pulls/"><button class="btn btn-primary pull-right">确定</button></a>
  </div>
</div>

<div id="id_merge_action">
<ul class="nav nav-tabs">
  <li class="active"><a href="#summary" data-toggle="tab">简介</a></li>
  <li><a href="#pullRequestCommits" class="cPullRequestAction" data-toggle="tab" data-action="commit">提交</a></li>
  <li><a href="#pullRequestDiff" class="cPullRequestAction" data-toggle="tab" data-action="diff">代码差异</a></li>
</ul>

<div class="tab-content pulls">
  <section class="tab-pane active" id="summary">
    <div class="refs">
      <p class="alert alert-info"><span class="pull-id">#{{pullRequest.id}}</span>
      请求把<span class="label label-success label-mini text-shadow-reverse">
      {{pullRequest.source_repo.name}}:{{pullRequest.source_refname}}
      </span>合并到<span class="label label-success label-mini text-shadow-reverse">
      {{pullRequest.desc_repo.name}}:{{pullRequest.desc_refname}}
      </span></p>
    </div>
    <div class="pull">
      <figure class="avatar">
        <img src="https://gravatar.com/avatar/{{pullRequest.desc_repo.username}}?s=55" alt="{{pullRequest.desc_repo.username}}">
      </figure>
      <div class="detail">
        <h3 class="heading">{{pullRequest.title}}</h3>
        <div class="desc">
          <p>{{pullRequest.desc}}</p>
        </div>

        {% if has_repo_pull_action_right %}
        <div class="actions">
          <a id="id_pull_merge" class="btn btn-primary" href="javascript: void(0)">开始合并</a>
          <a id="id_pull_reject" class="btn btn-danger" href="javascript: void(0)">拒绝</a>
          <a id="id_pull_close" class="btn btn-inverse" href="javascript: void(0)">关闭</a>
          <input type="hidden" value="{{ csrf_token }}" name="csrfmiddlewaretoken">
        </div>
        {% endif %}
      </div>
    </div>
  </section>

  <section class="tab-pane" id="pullRequestCommits"></section>

  <section class="tab-pane" id="pullRequestDiff"></section>

</div>
</div>

{% endblock %}

            {% block subjs %}
            <script>
              $(function(){
                var global_repo_branch_map = {};

                $('a[data-toggle="tooltip"]').tooltip();
                //$('.nav-tabs').tabs();

                var repoComparer = new RepoComparer('{{user_name}}', '{{repo_name}}', '', '');

                repoComparer.setPullUsername('{{pullRequest.source_repo.username}}', '{{pullRequest.desc_repo.username}}');
                repoComparer.from_refs = '{{pullRequest.source_refname}}';
                repoComparer.to_refs = '{{pullRequest.desc_refname}}';
                //repoComparer.renderCompare($('#commitsDiff'), null);

                repoComparer.registerLineContextEvent($('#pullRequestDiff'));
                $('.cPullRequestAction').click(
                  function() {
                    var action = $(this).data('action');
                    $('.ajaxLoader').show();
                    if(action == 'commit') {
                      repoComparer.loadCommits($('#pullRequestCommits'), null, null, null);
                    } else if(action == 'diff') {
                      repoComparer.loadDiff($('#pullRequestDiff'), null, null);
                    }
                  }
                );

                $('#id_pull_merge').click(function(){
                  $('#id_merge_output').show();
                  var i = 0;
                  var interval = setInterval(function() {
                    i = i + 1;
                    var j = i % 4;
                    var html = '正在进行 Pull Request，请耐心等候'
                    for (var k = 0; k < j; k++) {
                      html = html + '.'
                    }
                  $('#id_merge_output').html(html);
                  }, 500);
                  $('html, body').animate({ scrollTop: 0 }, 'fast');
                  $('#id_merge_process').show();
                  $('#id_merge_action').hide();
                  $.post('/{{user_name}}/{{repo_name}}/pull/{{pullRequest.id}}/merge/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
                    if(json.result == 'success') {
                      clearInterval(interval);
                      $('#id_merge_output').html(json.output);
                      if(json.returncode != 0) {
                        $('#id_merge_after').attr('href', '/{{user_name}}/{{repo_name}}/pull/{{pullRequest.id}}/');
                        $('#id_merge_after > button').removeClass('btn-primary').addClass('btn-danger').text('合并失败，请检查是否 存在冲突 或者 non-fast-forward');
                      }
                      $('#id_merge_after').show();
                    }
                  });
                  return false;
                });
                $('#id_pull_reject').click(function(){
                  $.post('/{{user_name}}/{{repo_name}}/pull/{{pullRequest.id}}/reject/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
                    if(json.result == 'success') {
                      window.location.href = '/{{user_name}}/{{repo_name}}/pulls/';
                    }
                  });
                  return false;
                });
                $('#id_pull_close').click(function(){
                  $.post('/{{user_name}}/{{repo_name}}/pull/{{pullRequest.id}}/close/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
                    if(json.result == 'success') {
                      window.location.href = '/{{user_name}}/{{repo_name}}/pulls/';
                    }
                  });
                  return false;
                });
              });
            </script>
            {% endblock %}
